﻿#region Copyright and License Notice
// Copyright (C)2010-2012 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>.
#endregion
using System.IO;
using System.Reflection;
using System.Text;
using ConfigGen.Utilities;
using ConfigGen.Utilities.Extensions.System.Reflection;
using NUnit.Framework;

namespace ConfigGen.Core.Tests.EndToEndTests
{
    [TestFixture]
    public class FileOutputEncodingEndToEndTests
    {
        [Test]
        public void OutputEncodingSetToASCII()
        {
            using (var testDirectory = new DisposableDirectory())
            {
                string settingsFile = Path.Combine(testDirectory.FullName, "App.Config.Settings.xls");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.Simple.App.Config.Settings.xls", settingsFile);

                string templateFile = Path.Combine(testDirectory.FullName, "App.Config.Template.xml");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.Simple.App.Config.Template.xml", templateFile);

                var settings = new Preferences
                {
                    SettingsFile = settingsFile,
                    TemplateFile = templateFile,
                    OutputDirectory = Path.Combine(testDirectory.FullName, "Configs"),
                    OutputEncoding = "A",
                    RemoveRootElement = true
                };

                var results = TestHelper.ExecuteCfgTest(settings);

                TestHelper.CheckExitResults(results, false);

                var generatedFile = Path.Combine(testDirectory.FullName, "Configs\\DevServer\\App.Config");

                var encoding = DetectEncoding(generatedFile);
                Assert.IsNull(encoding);
            }
        }

        [Test]
        public void OutputEncodingSetToUTF8()
        {
            using (var testDirectory = new DisposableDirectory())
            {
                string settingsFile = Path.Combine(testDirectory.FullName, "App.Config.Settings.xls");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.Simple.App.Config.Settings.xls", settingsFile);

                string templateFile = Path.Combine(testDirectory.FullName, "App.Config.Template.xml");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.Simple.App.Config.Template.xml", templateFile);

                var settings = new Preferences
                {
                    SettingsFile = settingsFile,
                    TemplateFile = templateFile,
                    OutputDirectory = Path.Combine(testDirectory.FullName, "Configs"),
                    OutputEncoding = "U8",
                    RemoveRootElement = true
                };

                var results = TestHelper.ExecuteCfgTest(settings);

                TestHelper.CheckExitResults(results, false);

                var generatedFile = Path.Combine(testDirectory.FullName, "Configs\\DevServer\\App.Config");

                var encoding = DetectEncoding(generatedFile);
                Assert.AreEqual(Encoding.UTF8, encoding);
            }
        }

        [Test]
        public void OutputEncodingSetToUTF16()
        {
            using (var testDirectory = new DisposableDirectory())
            {
                string settingsFile = Path.Combine(testDirectory.FullName, "App.Config.Settings.xls");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.Simple.App.Config.Settings.xls", settingsFile);

                string templateFile = Path.Combine(testDirectory.FullName, "App.Config.Template.xml");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.Simple.App.Config.Template.xml", templateFile);

                var settings = new Preferences
                {
                    SettingsFile = settingsFile,
                    TemplateFile = templateFile,
                    OutputDirectory = Path.Combine(testDirectory.FullName, "Configs"),
                    OutputEncoding = "U16",
                    RemoveRootElement = true
                };

                var results = TestHelper.ExecuteCfgTest(settings);

                TestHelper.CheckExitResults(results, false);

                var generatedFile = Path.Combine(testDirectory.FullName, "Configs\\DevServer\\App.Config");

                var encoding = DetectEncoding(generatedFile);
                Assert.AreEqual(Encoding.Unicode, encoding);
            }
        }

        public Encoding DetectEncoding(string fileName)
        {
            using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var bits = new byte[3];
                fs.Read(bits, 0, 3);

                // UTF8 BOM is like 0xEF,0xBB,0xBF
                if (bits[0] == 0xEF && bits[1] == 0xBB && bits[2] == 0xBF)
                {
                    return Encoding.UTF8;
                }

                //UTF16 BOM is like 0xFF, 0xFE
                if (bits[0] == 0xFF && bits[1] == 0xFE)
                {
                    return Encoding.Unicode;
                }

                return null;
            }
        }
    }
}
